package gmomo.test.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.packet.Presence.Type;

public class ContactList extends JScrollPane{
	
	private static final long serialVersionUID = -4880896884002085395L;
	
	private final static DefaultMutableTreeNode root = new DefaultMutableTreeNode("Contacts");
	
	private ContactGUI contactGUI;
	private DefaultTreeModel model;
	private Map<String, DefaultMutableTreeNode> mapContact;
	
	private MainFrame main;
	private boolean firstOpening = true;
	
	public ContactList(MainFrame main) {
		this.main = main;
		mapContact = Collections.synchronizedMap(new HashMap<String, DefaultMutableTreeNode>());
		setupContactGUI();
		setViewportView(contactGUI);
	}
	
	private void setupContactGUI() {
		model = new DefaultTreeModel(root);
		contactGUI = new ContactGUI(model);
		
		contactGUI.addMouseListener(
				new MouseAdapter() {
					public void mousePressed(MouseEvent e) {
						if ( e.getClickCount() == 2 ) {
							TreePath path = contactGUI.getPathForLocation(e.getX(), e.getY());
							if ( path != null ) {
								DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
								if ( node != null ) {
									Object obj = node.getUserObject();
									if ( obj instanceof Contact ) {
										Contact contact = (Contact)obj;
										System.out.println( "Found: " + contact.toString() );
//										new InvitationDialog(main, contact.getBareAddress());
//										new MessageFrame(main, contact);
										main.updateMsgWin(contact);
									}
								}
							}
						}
					}
				}
				);
	}
	
	public void updateContact(Presence presence) {
		String xmppAddress = presence.getFrom();
		String name = StringUtils.parseName(xmppAddress);
		Type type = presence.getType();
		
		DefaultMutableTreeNode child = mapContact.get( xmppAddress ); 
		
		if ( child != null ) {
			if ( type == Type.unavailable ) {
				System.out.println(xmppAddress+": un-available");
				mapContact.remove( xmppAddress );
				model.removeNodeFromParent( child );
			}
		}else {
			if ( type == Type.available ) {
				try {
					Contact contact = new Contact( xmppAddress );
					System.out.println(xmppAddress+": available");
					child = new DefaultMutableTreeNode(contact);
					model.insertNodeInto(child, root, root.getChildCount());
					mapContact.put(xmppAddress, child);
				
					if ( !contactGUI.isExpanded( 0 ) ) {
						contactGUI.expandRow( 0 );
					}
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void clear() {
		root.removeAllChildren();
		mapContact.clear();
		model.reload();
	}


	
}//end of class ContactList 
